package org.linlinjava.litemall.admin.job;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.linlinjava.litemall.db.domain.LitemallIntegralLog;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.service.LitemallIntegralLogService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Component
@Slf4j
@RequiredArgsConstructor
public class IntegralLogJob {

    private final LitemallIntegralLogService litemallIntegralLogService;

    private final LitemallUserService litemallUserService;



    /**
     * 检查积分到期时间
     */
    //@Scheduled(cron = "0 0 0 * * ?")
    public void checkTheCreditDueTime(){
        BigDecimal totalIntegral = new BigDecimal(0.00);
        for (LitemallIntegralLog litemallIntegralLog : litemallIntegralLogService.list()) {
            //判断今天是否是添加积分的第30天
            if(LocalDateTime.now().isBefore(litemallIntegralLog.getAddTime().plusDays(30))){
                LitemallUser litemallUser = litemallUserService.findById(litemallIntegralLog.getUserId());
                litemallUser.setPoints(litemallUser.getPoints().subtract(litemallIntegralLog.getChangePoints()));
                litemallUserService.updateById2(litemallUser);
            }
        }

    }

}
